package cn.baiweigang.uu.api.linearswap;

import cn.baiweigang.uu.api.BaseApi;
import cn.baiweigang.uu.enums.DirectionEnum;
import cn.baiweigang.uu.enums.MarginModeEnum;
import cn.baiweigang.uu.model.AccountInfo;
import cn.baiweigang.uu.model.PositionInfo;
import cn.baiweigang.uu.utils.LogUtil;
import com.alibaba.fastjson.JSONObject;
import com.huobi.linearswap.api.response.account.SwapAccountPositionInfoResponse;
import com.huobi.linearswap.api.service.account.AccountAPIServiceImpl;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class AccountApi extends BaseApi {

    public void updateAccountInfoFromHB(AccountInfo accountInfo) {
        //需要apikey
        loadByAccountName(accountInfo.getAccountName());
        //请求平台接口
        if (accountInfo.getMarginMode() == MarginModeEnum.ISOLATED) {
            updateSwapAccountPositionInfo(accountInfo);
        } else {
            LogUtil.logError("第一版只支持逐仓账户");
        }
    }

    /**
     * 火币-逐仓账户查询
     *
     * @param accountInfo
     */
    private void updateSwapAccountPositionInfo(AccountInfo accountInfo) {
        AccountAPIServiceImpl huobiAPIService =
                new AccountAPIServiceImpl(apiKeyInfo.getApiKey(), apiKeyInfo.getSecretKey());
        SwapAccountPositionInfoResponse response = huobiAPIService.getSwapAccountPositionInfo(accountInfo.getContractCode().getCode());
        if (response != null && response.getStatus().equals("ok")) {
            accountInfo.setTs(response.getTs());
            SwapAccountPositionInfoResponse.DataBean dataBean = response.getData().get(0);
            accountInfo.setMarginAccount(dataBean.getMarginAccount());
            accountInfo.setLeverRate(dataBean.getLeverRate().intValue());
            accountInfo.setMarginAvailable(dataBean.getMarginAvailable());
            accountInfo.setMarginBalance(dataBean.getMarginBalance());
            accountInfo.setMarginFrozen(dataBean.getMarginFrozen());
            accountInfo.setMarginPosition(dataBean.getMarginPosition());
            accountInfo.setRiskRate(dataBean.getRiskRate());
            accountInfo.setLiquidationPrice(dataBean.getLiquidationPrice());
            accountInfo.setPositionInfo(convert2PositionInfo(dataBean.getPositions()));
            LogUtil.logInfo("更新账户信息成功");
            LogUtil.logInfo(accountInfo);
        } else {
            LogUtil.logError("查询账户信息失败 " + JSONObject.toJSONString(response));
        }
    }

    private List<PositionInfo> convert2PositionInfo(List<SwapAccountPositionInfoResponse.DataBean.Positions> positions) {
        List<PositionInfo> positionInfoList = new ArrayList<>();
        if (positions != null && positions.size() > 0) {
            for (SwapAccountPositionInfoResponse.DataBean.Positions p : positions) {
                PositionInfo positionInfo = new PositionInfo();
                positionInfo.setPositionMargin(p.getPositionMargin());
                positionInfo.setVolume(p.getVolume().intValue());
                positionInfo.setAvailable(p.getAvailable().intValue());
                positionInfo.setCostHold(p.getCostHold());
                positionInfo.setDirection(DirectionEnum.byCode(p.getDirection()));
                positionInfo.setFrozen(p.getFrozen().intValue());
                positionInfo.setCostOpen(p.getCostOpen());
                positionInfo.setLastPrice(p.getLastPrice());
                positionInfo.setProfitUnreal(p.getProfitUnreal());
                positionInfo.setProfit(p.getProfit());
                positionInfo.setProfitRate(p.getProfitRate());
                positionInfoList.add(positionInfo);
            }
        }
        return positionInfoList;
    }
}
